<template>
  <div class="xtx-goods-page" v-if="goods.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">
          {{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">
          {{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem to="/">
          {{ goods.name }}
        </XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures" />
          <!-- 销量组件 -->
          <GoodsSale :goods="goods"/>
        </div>
        <div class="spec">
          <!-- 信息组件 -->
          <GoodsName :goods="goods"/>
          <!-- sku组件(规格组件) -->
            <goods-sku
              :goods="goods"
              @change="skuChange"
            ></goods-sku>
            <!-- 数量计算 -->
            <XtxNumbox :max="999" :min="1" v-model="curNum" />
            <!-- 加入购物车按钮 -->
             <XtxButton type="primary" style="margin-top:20px;" @click="add">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs/>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
            <GoodsHot :hotType="1" />
            <GoodsHot :hotType="2" />
            <GoodsHot :hotType="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import Message from '@/components/Message/index.js'
import GoodsImage from './components/goods-image'
import GoodsSale from './components/goods-sale'
import GoodsName from './components/goods-name'
import GoodsSku from './Sku/index'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSale,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot
  },
  setup () {
    const goods = ref({})
    const route = useRoute()
    // 商品数量
    const curNum = ref(1)
    async function getGoods () {
      const res = await findGoods(route.params.id)
      goods.value = res.result
      // console.log('后台返回的商品详情数据', goods.value)
      // console.log(goods.value.specs)
      // 尺码规格排序
      if (goods.value.specs) {
        goods.value.specs.forEach(item => {
          if (item.name === '尺码') {
            const size = item.values.map(item => item.name).sort()
            // console.log('筛选出所有的尺码', size)
            // 根据排序后的name查找对应的对象添加进一个新数组
            const arr = []
            size.map(i => {
              // console.log(i)
              // console.log(item.values)
              const newArr = item.values.filter(item => item.name === i)
              arr.push(...newArr)
            })
            item.values = arr
          }
        })
      }
    }
    onMounted(() => {
      getGoods()
    })
    const currSku = ref({}) // 当前选中的有效的sku
    function skuChange (sku) {
      // console.log(sku)
      // 保存用户选中的sku详情
      currSku.value = sku
    }
    const store = useStore()
    // 加入购物车
    function add () {
      // 如果sku无效直接return
      if (!currSku.value.skuId) Message({ type: 'warn', text: '请完善商品规格' })
      // console.log('用户加入购物车的商品数量', curNum.value)
      const good = {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.price,
        nowPrice: currSku.value.price,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true,
        isEffective: true,
        count: curNum.value
      }
      // 触发actions函数把数据存入vuex
      store.dispatch('cart/asyncInsertCart', good)
    }
    return {
      goods,
      skuChange,
      curNum,
      add
    }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
